'use strict';
app.controller('advertisementTypeCtrl',['$scope','utilService','$modal', function($scope,utilService,$modal){
    $scope.advertisementType = {};
    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'advertise/type',
            params:$scope.advertisementType
        }).then(function(result){
        });
    }
    $scope.query();

    //打开添加框
    $scope.openAdd = function () {
        var modalInstance = $modal.open({
            templateUrl: 'advertisementTypeAdd.html',
            controller: 'industrySelectCtrl',
            size: 'sm',
            scope:$scope,
            resolve: {
                items: function () {
                    return  $scope;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            console.log($scope.selected = selectedItem);
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };


    $scope.openEdit = function (id,name) {
        var modalInstance = $modal.open({
            templateUrl: 'advertisementTypeEdit.html',
            controller: 'advertisementTypeEditCtrl',
            size: 'sm',
            scope:$scope,
            resolve: {
                data:function(){
                    var advertisementType={};
                    advertisementType.id=id;
                    advertisementType.name=name;
                    return advertisementType;
                }
            }
        });

        modalInstance.result.then(function (result) {
            console.log(result);
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });

    };


    $scope.openDelete = function (id,name) {
        var modalInstance = $modal.open({
            templateUrl: 'advertisementTypeDelete.html',
            controller: 'advertisementTypeDeleteCtrl',
            scope:$scope,
            size: 'sm',
            resolve: {
                data:function(){
                    var data={};
                    data.id=id;
                    data.name=name;
                    return data;
                }
            }
        });

        modalInstance.result.then(function (result) {
            console.log(result);
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });

    };

}]);


app.controller('industrySelectCtrl', ['$scope', '$modalInstance', 'items', function($scope, $modalInstance, items) {
    $scope.items = items;
    $scope.selected = {
        item: $scope.items[0]
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('close');
    };
}]);


app.controller('advertisementTypeAddCtrl', ['$scope','$state','advertisementTypeService','toaster',
                                            function($scope,$state,advertisementTypeService,toaster) {
    $scope.advertisementTypeName={};
    $scope.AllowToAdd=true;
    $scope.add=function(){
        if(!$scope.AllowToAdd){
            return;
        }
        $scope.AllowToAdd=false;
        advertisementTypeService.add($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.cancel();
                    toaster.pop('success', "提示", "操作成功");
                    $scope.AllowToAdd=true;
                    $state.reload('app.advertisement');
                    break;
                case 606:
                case 607:
                    toaster.pop('waiting', "提示", result.message);
                    $scope.AllowToAdd=true;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    $scope.AllowToAdd=true;
                    break;
            }
        });
    }
}]);


app.controller('advertisementTypeEditCtrl', ['$scope', '$modalInstance','$state','advertisementTypeService','toaster','data',
    function($scope, $modalInstance,$state,advertisementTypeService,toaster,data) {
        $scope.data=data;


        $scope.AllowToEdit=true;
        $scope.edit =function(){
            if(!$scope.AllowToEdit){
                return;
            }
            $scope.AllowToEdit=false;

            advertisementTypeService.update($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $scope.cancel();
                        toaster.pop('success', "提示", "操作成功");
                        $scope.AllowToEdit=true;
                        $state.reload('app.advertisement');
                        break;
                    case 607:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.AllowToEdit=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToEdit=true;
                        break;
                }
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };

    }]);

//删除模态框Controller
app.controller('advertisementTypeDeleteCtrl', ['$scope', '$modalInstance','$state','advertisementTypeService','toaster','data',
    function($scope, $modalInstance,$state,advertisementTypeService,toaster,data) {
        $scope.data=data;

        $scope.AllowToDelete=true;

        //确认删除
        $scope.ok=function(){
            if(!$scope.AllowToDelete){
                return;
            }

            $scope.AllowToDelete=false;

            var id=$scope.data.id;
            advertisementTypeService.delete($scope,id).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $state.reload('app.advertisement');
                        $scope.AllowToDelete=true;
                        break;
                    case 607:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.AllowToDelete=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToDelete=true;
                        break;
                }
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
}]);